package com.qfedu.fmmall.item.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qfedu.fmmall.entity.OrderItem;
import com.qfedu.fmmall.item.dao.OrderItemDao;
import com.qfedu.fmmall.item.service.OrderItemAddService;
import com.qfedu.fmmall.vo.ShoppingCartVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author : yanBin
 * @date :2022/1/25 15:06
 */
@Service
public class OrderItemAddServiceImpl implements OrderItemAddService {
    private static final Logger log = LoggerFactory.getLogger(OrderItemAddServiceImpl.class);

    @Autowired
    private OrderItemDao orderItemDao;

    @Override
    public int addItem(List<ShoppingCartVO> shoppingCartVOList, String orderId) {
        int i = -1;
        try {
            //添加到订单快照表order_item
            for (ShoppingCartVO sc : shoppingCartVOList) {
                int cnm = Integer.valueOf(sc.getCartNum());
                String itemId = UUID.randomUUID().toString().replace("-", "");
                OrderItem orderItem = new OrderItem(itemId, orderId, sc.getProductId(), sc.getProductName(),
                        sc.getProductImg(), sc.getSkuId(), sc.getSkuName(), new BigDecimal(sc.getSellPrice())
                        , cnm, new BigDecimal(cnm * sc.getSellPrice()), new Date(), new Date(), 0);
                i = orderItemDao.insertSelective(orderItem);

            }

        } catch (Exception e) {
            log.error("创建订单快照失败", e.getLocalizedMessage());
            return i;
        }
        return i;
    }
}
